package com.tjsinfo.tjsoa.efile.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.efile.vo.Efile;
import com.tjsinfo.tjsoa.efile.dao.IEfileDao;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.User;

public class FileManageUploadSaveAction extends Action {
	private IEfileDao efileDao;
	
	public IEfileDao getEfileDao() {
		return efileDao;
	}

	public void setEfileDao(IEfileDao efileDao) {
		this.efileDao = efileDao;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			String cid = request.getParameter("cid");
			String type = request.getParameter("type");
			Date date = new Date();
			DateFormat sdf = new SimpleDateFormat("yyyy-M-d");
			String createDate = sdf.format(date);
			
			HttpSession session=request.getSession();
			User user=(User)session.getAttribute("user");
			String userName=user.getUsername();
//			String tomcatPath = new File(".").getCanonicalPath();
//			String path = tomcatPath.substring(0,tomcatPath.length()-4);
			String fromFilePath=ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
//			String toFilePath=ToolKit.getProperties("com/tjsinfo/tjsoa/efile/properties/systemResource.properties", this).getProperty("efile.fileurl");
			String toFileUrl=ToolKit.getProperties("com/tjsinfo/tjsoa/efile/properties/systemResource.properties", this).getProperty("efile.toFileUrl");
			String downUrl=ToolKit.getProperties("com/tjsinfo/tjsoa/efile/properties/systemResource.properties", this).getProperty("efile.downUrl");
			String toFilePath=toFileUrl;
			
			System.out.println("fromFilePath:"+fromFilePath+"   toFileUrl:"+toFileUrl);
			
//			private static final Log log = LogFactory.getLog(EfileDao.class);
//			log.debug("fromFilePath:"+fromFilePath+"   toFileUrl:"+toFileUrl);
			
			List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);
			String fileType = null;
			String fileSize = "";
			String fileName = "";
			
			List<Efile> list = new ArrayList<Efile>();
			for (Files files : fileList) {
				Efile efile = new Efile();
				fileName = files.oldFileName;
				fileType = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());//文件后缀
				
				efile.setAllPath(downUrl + files.newFileName);
				efile.setCfileName(fileName);
				efile.setCid(cid);
				efile.setFileName(files.newFileName);
				efile.setUrl(toFilePath);
				efile.setTitle("");
				efile.setRemark(createDate);
				efileDao.save(efile);
			}	
			
		    request.setAttribute("cid",cid);
		    request.setAttribute("type", type);
			return mapping.findForward("success");
        } catch (Exception e) {
            e.printStackTrace();
			return null;
        } 
	}
}
